import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores/index'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      component: () => import('@/views/login/loginIndex.vue'),
    },
    {
      path: '/',
      redirect: '/login',
    },
    {
      path: '/changePassword',
      component: () => import('@/views/page/changePassword.vue'),
    },
    {
      path: '/management',
      redirect: '/management/work',
      component: () => import('@/views/layout/layoutIndex.vue'),
      children: [
        {
          path: '/management/work',
          component: () => import('@/views/page/workIndex.vue'),
        },
        {
          path: '/management/staff',
          component: () => import('@/views/page/staffManagement.vue'),
        },
        {
          path: '/management/package',
          component: () => import('@/views/page/packageManagement.vue'),
        },
        {
          path: '/management/order',
          component: () => import('@/views/page/orderManagementIndex.vue'),
        },
        {
          path: '/management/food',
          component: () => import('@/views/page/foodManagement.vue'),
        },
        {
          path: '/management/data',
          component: () => import('@/views/page/dataIndex.vue'),
        },
        {
          path: '/management/classification',
          component: () => import('@/views/page/classificationManagement.vue'),
        },
      ],
    },
    {
      path: '/addFood',
      component: () => import('@/views/page/addFood.vue'),
    },
    {
      path: '/addPackage',
      component: () => import('@/views/page/addPackage.vue'),
    },
    {
      path: '/addPackageDish',
      component: () => import('@/views/page/addPackageDish.vue'),
    },
    {
      path: '/orderDetail',
      component: () => import('@/views/page/viewPackageDetail.vue'),
    },
  ],
})

//  前置路由守卫

router.beforeEach((to) => {
  const userStore = useUserStore()
  if (!userStore.token) {
    if (to.path !== '/login') {
      return '/login'
    }
  }
})
export default router
